<template>
  <div>
    <!--添加按钮-->
    <el-row>
      <el-col :span="20" align="right">
        <el-button type="primary" icon="el-icon-plus" @click="showDia()">添加</el-button>
      </el-col>
    </el-row>
    <!--列表显示-->
    <el-table
      ref="multipleTable"
      :data="typeList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
        align="center">
      </el-table-column>
      <el-table-column
        label="类型名"
        width="550"
        align="center">
        <template slot-scope="scope">
          <el-input @blur="saveEditedType(scope.row)" size="medium " v-if="editRaw == scope.$index" v-model="type.typeName" auto-complete="off"></el-input>
          <span v-if="editRaw != scope.$index" @click="toggleShow(scope.$index,scope.row)">{{ scope.row.typeName }}</span>
        </template>
      </el-table-column>
      <!--操作区域-->
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-card>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        background
        :page-sizes="[10, 20, 30, 40]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next,jumper"
        :total="page.total">
      </el-pagination>
    </el-card>

    <!--添加类型弹窗-->
    <el-dialog title="创建房间类型" :visible.sync="dialogFormVisible">
      <el-form ref="typeForm" v-model="type">
        <el-form-item label="房间类型名">
          <el-input v-model="type.typeName" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addType">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getTypesForPage, deleteTypes, createType, getTypes, saveEditedType} from "@/api/type";

  export default {
    data(){
      return{
        typeList:[],//房间列表
        page:{
          pageNum:1,
          pageSize:10,
          total: 40
        },//页面信息
        multipleSelection: [],//多选操作
        type:{
          id:null,
          typeName:""
        },//房类型
        dialogFormVisible:false,//弹窗显示
        editRaw:-1,//是否处于修改状态
      }
    },
    methods:{
      /**
       * 获取类型列表
       */
      getTypes(){
        getTypesForPage(this.page.pageNum,this.page.pageSize).then(res => {
          this.typeList = res.data.typeList;
          this.page.total = res.data.total;
        })
      },
      /**
       * 修改每页显示条数
       * @param pageSize
       */
      handleSizeChange(pageSize){
        this.page.pageSize = pageSize;
        this.getTypes();
      },
      /**
       * 修改页码
       * @param pageNum
       */
      handleCurrentChange(pageNum){
        this.page.pageNum = pageNum;
        this.getTypes();
      },
      /**
       * 删除类型
       */
      handleDelete(index,type){
        let tarType = this.multipleSelection;
        if (tarType != null) {
          tarType = [type];
        }

        deleteTypes(tarType).then(res => {
          if (res.code == 20000) {
            this.$message.success(res.message);
            this.getTypes();
          }else{
            this.$message.error(res.message);
          }
        })
      },
      /**
       * 获取当前所选对象
       * @param val
       */
      handleSelectionChange(val){
        this.multipleSelection = val;
      },
      /**
       * 展示弹窗
       */
      showDia(){
        this.dialogFormVisible = true;
        this.type.typeName = "";
      },
      /**
       * 添加类型
       */
      addType(){
        createType(this.type.typeName).then(res => {
          if (res.code == 20000) {
            this.$message.success(res.message);
            this.dialogFormVisible = false;
            this.getTypes();
          }else {
            this.$message.error(res.message);
          }
        })
      },
      /**
       * 切换房类型显示
       * @param index
       * @param type
       */
      toggleShow(index,type){
        this.type.typeName = type.typeName;
        this.editRaw = index;
      },
      /**
       * 保存修改的房类型
       * @param type
       */
      saveEditedType(type){
        let flag = true;

        //验证
        if(this.type.typeName == ""){
          flag = false;
          this.$message.error("房间类型名不能为空你个**~~");
        }else if (type.typeName == this.type.typeName) {
          flag = false;
        }

        if (flag) {
          this.type.id = type.id;
          saveEditedType(this.type).then(res => {
            if (res.code === 20000) {
              this.getTypes();
              this.$message.success(res.message);
              this.$message.error(res.message);
            }
          })
        }

        this.editRaw = -1;
      }
    },
    mounted() {
      this.getTypes();
    }
  }
</script>

<style scoped lang="scss">

</style>
